package com.dldata.devtools30.controller.zhibiao;

import com.dldata.devtools30.controller.DevBase30Controller;
import com.dldata.devtools30.entity.DevtApiTypeEntity;
import com.dldata.devtools30.entity.DevtZbclassInfoEntity;
import com.dldata.devtools30.repository.zhibiao.DevtZbclassInfoEntityDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/DevtZbclassInfoEntityController")
public class DevtZbclassInfoEntityController extends DevBase30Controller {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    DevtZbclassInfoEntityDao zbclassInfoEntityDao;

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
        String serach="%"+paramMap.getOrDefault("serach","")+"%";
        List<DevtZbclassInfoEntity> metadataTypeEntities = zbclassInfoEntityDao.getAll(serach);
       
        return Result.ok(metadataTypeEntities);
    }
    
    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtZbclassInfoEntity entity){
        if(StringUtil.isEmptyOrLength0(entity.getZbclassname())){
            return Result.error("指标分类名称不能为空");
        }

        if(StringUtil.isEmptyOrLength0(entity.getId())){
            entity.setId(UUID.randomUUID().toString());
        }
        zbclassInfoEntityDao.save(entity);

        return Result.ok();
    }
    
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){

        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            DevtZbclassInfoEntity devtZbclassInfoEntity = zbclassInfoEntityDao.findById(id).orElse(null);
            if(devtZbclassInfoEntity!=null){
                zbclassInfoEntityDao.delete(devtZbclassInfoEntity);
            }
        }
        catch (Exception ex){
            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
    
    @RequestMapping("edit")
    public String toEdit(String zbclassid, Model model){
        DevtZbclassInfoEntity zbobjInfoEntity = new DevtZbclassInfoEntity();
        if(!StringUtil.isEmptyOrLength0(zbclassid)){
            zbobjInfoEntity = zbclassInfoEntityDao.findById(zbclassid).orElse(new DevtZbclassInfoEntity());
        }
        model.addAttribute("entity",zbobjInfoEntity);
    
        List<DevtZbclassInfoEntity> zbclassInfoEntityList = zbclassInfoEntityDao.getAllByParentid("0");
        DevtZbclassInfoEntity devtZbclassInfoEntity = new DevtZbclassInfoEntity();
        devtZbclassInfoEntity.setId("0");
        devtZbclassInfoEntity.setZbclassname("一级分类");
        zbclassInfoEntityList.add(0, devtZbclassInfoEntity);
        model.addAttribute("zbclassList",zbclassInfoEntityList);
        
        return "thymeleaf/devtools30/zhibiao/v_zbclassInfoEdit";
    }
}
